package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.bo.AdminConfigExpressBo;
import com.cskaoyan.market.bo.AdminConfigOrderBo;
import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.service.AdminConfigService;
import com.cskaoyan.market.util.MybatisUtils;
import com.cskaoyan.market.vo.AdminConfigExpressVo;
import com.cskaoyan.market.vo.AdminConfigOrderVo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminConfigServiceImpl implements AdminConfigService {


    @Override
    public void postExpress(AdminConfigExpressBo adminConfigExpressBo) {

        //该函数通过adminConfigExpressBo参数获取管理员配置的快递费用最小值和实际值，
        // 然后使用Mybatis操作数据库，
        // 根据最小值和实际值更新数据库中相应的market_express_freight_min和market_express_freight_value的值。
        // 最后提交事务并关闭会话。

        SqlSession session = MybatisUtils.getSession();

        //这两个函数分别获取了管理员配置的快递费用的最小值和实际值。
        String marketExpressFreightMin = adminConfigExpressBo.getMarket_express_freight_min();
        String marketExpressFreightValue = adminConfigExpressBo.getMarket_express_freight_value();

        MarketSystem marketSystemByMin = new MarketSystem();
        marketSystemByMin.setKeyName("market_express_freight_min");

        MarketSystem marketSystemByValue = new MarketSystem();
        marketSystemByValue.setKeyName("market_express_freight_value");

        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        MarketSystemExample marketSystemExampleByMin = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleCriteriaByMin = marketSystemExampleByMin.createCriteria();
        marketSystemExampleCriteriaByMin.andKeyNameEqualTo("market_express_freight_min");
        marketSystemMapper.updateByExampleSelective(marketSystemByMin,marketSystemExampleByMin);

        MarketSystemExample marketSystemExampleByValue = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleCriteriaByValue  = marketSystemExampleByValue.createCriteria().andKeyNameEqualTo("market_express_freight_value");
        marketSystemExampleCriteriaByValue.andKeyNameEqualTo("market_express_freight_value");
        marketSystemMapper.updateByExampleSelective(marketSystemByValue,marketSystemExampleByValue);


        session.commit();
        session.close();

    }

    @Override
    public void postOrder(AdminConfigOrderBo adminConfigOrderBo) {
        SqlSession session = MybatisUtils.getSession();

        String marketOrderUnconfirm = adminConfigOrderBo.getMarket_order_unconfirm();
        String marketOrderUnpaid = adminConfigOrderBo.getMarket_order_unpaid();
        String marketOrderComment = adminConfigOrderBo.getMarket_order_comment();

        MarketSystem marketSystemToUnconfirm = new MarketSystem();
        marketSystemToUnconfirm.setKeyValue(marketOrderUnconfirm);
        MarketSystem marketSystemToUnpaid = new MarketSystem();
        marketSystemToUnpaid.setKeyValue(marketOrderUnpaid);
        MarketSystem marketSystemToComment = new MarketSystem();
        marketSystemToComment.setKeyValue(marketOrderComment);

        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        MarketSystemExample marketSystemExampleToUnconfirm = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleToUnconfirmCriteria = marketSystemExampleToUnconfirm.createCriteria();
        marketSystemExampleToUnconfirmCriteria.andKeyNameEqualTo("market_order_unconfirm");
        marketSystemMapper.updateByExampleSelective(marketSystemToUnconfirm, marketSystemExampleToUnconfirm);

        MarketSystemExample marketSystemExampleToUnpaid = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleToUnpaidCriteria = marketSystemExampleToUnpaid.createCriteria();
        marketSystemExampleToUnpaidCriteria.andKeyNameEqualTo("market_order_unpaid");
        marketSystemMapper.updateByExampleSelective(marketSystemToUnpaid, marketSystemExampleToUnpaid);

        MarketSystemExample marketSystemExampleToComment = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleToCommentCriteria = marketSystemExampleToComment.createCriteria();
        marketSystemExampleToCommentCriteria.andKeyNameEqualTo("market_order_comment");
        marketSystemMapper.updateByExampleSelective(marketSystemToComment, marketSystemExampleToComment);

        session.commit();
        session.close();
    }

    @Override
    public AdminConfigOrderVo getOrder() {
        AdminConfigOrderVo adminConfigOrderVo = new AdminConfigOrderVo();
        SqlSession session = MybatisUtils.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        MarketSystemExample marketSystemExampleToUnconfirm = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleToUnconfirmCriteria = marketSystemExampleToUnconfirm.createCriteria();
        marketSystemExampleToUnconfirmCriteria.andKeyNameEqualTo("market_order_unconfirm");
        List<MarketSystem> marketSystemToUnconfirms = marketSystemMapper.selectByExample(marketSystemExampleToUnconfirm);
        adminConfigOrderVo.setMarket_order_unconfirm(marketSystemToUnconfirms.get(0).getKeyValue());

        MarketSystemExample marketSystemExampleToUnpaid = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleToUnpaidCriteria = marketSystemExampleToUnpaid.createCriteria();
        marketSystemExampleToUnpaidCriteria.andKeyNameEqualTo("market_order_unpaid");
        List<MarketSystem> marketSystemToUnpaids = marketSystemMapper.selectByExample(marketSystemExampleToUnpaid);
        adminConfigOrderVo.setMarket_order_unpaid(marketSystemToUnpaids.get(0).getKeyValue());

        MarketSystemExample marketSystemExampleToComment = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleToCommentCriteria = marketSystemExampleToComment.createCriteria();
        marketSystemExampleToCommentCriteria.andKeyNameEqualTo("market_order_comment");
        List<MarketSystem> marketSystemToComments = marketSystemMapper.selectByExample(marketSystemExampleToComment);
        adminConfigOrderVo.setMarket_order_comment(marketSystemToComments.get(0).getKeyValue());

        session.close();
        return adminConfigOrderVo;
    }

    @Override
    public AdminConfigExpressVo getExpress() {
        AdminConfigExpressVo adminConfigExpressVo = new AdminConfigExpressVo();
        SqlSession session = MybatisUtils.getSession();
        MarketSystemMapper marketSystemMapper = session.getMapper(MarketSystemMapper.class);

        MarketSystemExample marketSystemExampleToMin = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleToMinCriteria = marketSystemExampleToMin.createCriteria();
        marketSystemExampleToMinCriteria.andKeyNameEqualTo("market_express_freight_min");
        List<MarketSystem> marketSystemToMins = marketSystemMapper.selectByExample(marketSystemExampleToMin);
        MarketSystem marketSystemToMin = marketSystemToMins.get(0);
        adminConfigExpressVo.setMarket_express_freight_min(marketSystemToMin.getKeyValue());

        MarketSystemExample marketSystemExampleToValue = new MarketSystemExample();
        MarketSystemExample.Criteria marketSystemExampleToValueCriteria = marketSystemExampleToValue.createCriteria();
        marketSystemExampleToValueCriteria.andKeyNameEqualTo("market_express_freight_value");
        List<MarketSystem> marketSystemToValues = marketSystemMapper.selectByExample(marketSystemExampleToValue);
        MarketSystem marketSystemToValue = marketSystemToValues.get(0);
        adminConfigExpressVo.setMarket_express_freight_value(marketSystemToValue.getKeyValue());

        session.close();

        return adminConfigExpressVo;
    }


}
